home *** CD-ROM | disk | FTP | other *** search
- Path: engnews1.Eng.Sun.COM!taumet!clamage
- From: boukanov@sentef1.fi.uib.no (Igor Boukanov)
- Newsgroups: comp.std.c++
- Subject: ?s about exceptions in destructors
- Date: 2 Apr 1996 15:16:17 GMT
- Organization: Fysisk institutt, Universitetet i Bergen
- Approved: clamage@eng.sun.com (comp.std.c++)
- Message-ID: <4jrf96$1mj@ugress.uib.no>
- NNTP-Posting-Host: taumet.eng.sun.com
- Keywords: C++, destructor, exception
- X-Nntp-Posting-Host: sentef1.fi.uib.no
- X-Newsreader: TIN [version 1.2 PL2]
- Content-Length: 1323
- Originator: clamage@taumet
-
- Consider the following:
- struct A {
- ~A(){throw int(0);}
- };
-
- struct B {
- A a;
- ~B(){throw float(0.0);}
- };
-
- void f() { B b; }
-
- So according to the 09.95 DWP, 15.5.1.1 any call to f will call
- terminate().
-
- But what should happen in the next code:
-
- void f2() {
- char buf[sizeof(B)];
- B* b = new(buf) B;
- b->B::~B();
- };
-
- void g2() {
- try { f2(); } catch(float) { }
- }
-
- I suppose that g2 will catch float exception after "throw float(0.0);"
- in B::~B and A::~A of b->a will not be called because according to 15.2:
- " 1 As control passes from a throw-point to a handler, destructors are
- invoked for all automatic objects constructed since the try block was
- entered.
- 2 An object that is partially constructed will have destructors executed
- only for its fully constructed sub-objects..."
-
- and because b is NOT a automatic object so b->a is NOT a automatic object too.
-
- Is this right?
-
- And what should happen in the next:
- void f3() {
- B* b = new B;
- delete b;
- };
-
- Is it supposed that this lines can be rewritten as:
- void f4() {
- B* b = new B;
- try { b->B::~B(); }
- catch(...) { operator delete(b); throw; }
- operator delete(b);
- };
-
- So A::~A() for b->a will not be called again but memory will be released?
-
-
- --
- Regards, Igor Boukanov.
- igor.boukanov@fi.uib.no
- http://www.fi.uib.no/~boukanov/
-
-
- [ comp.std.c++ is moderated. To submit articles: try just posting with ]
- [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
- [ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
- [ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
- [ Comments? mailto:std-c++-request@ncar.ucar.edu ]
-